home *** CD-ROM | disk | FTP | other *** search
/ Flash MX Savvy / FlashMX Savvy.iso / pc / MAC / Amapi3D / Amapi3DTrial_Edition / 3SPACE / CameraRotateBehavior.js < prev    next >
Encoding:
Text File  |  2001-02-20  |  2.8 KB  |  116 lines  |  [AMAS/AMAP]

  1. // -* CameraRotateBehavior.js *-
  2. //
  3. // Name: CameraRotate behavior
  4. // Description: 
  5. // Author:
  6. // Version: $Id: CameraRotateBehavior.js,v 1.6 2000/12/21 15:03:30 consumer Exp $
  7. //
  8.  
  9. //
  10. // Private variables
  11. //
  12.  
  13. var phiLimit = 0.9 * Math.PI / 2;
  14. var phiMin = 20;
  15. var phiMax = 80;
  16.  
  17. //
  18. // Private functions
  19. //
  20.  
  21. var rotateCameras = new Array(1);
  22.  
  23. function CameraRotateBehavior(camera, intensity)
  24. {
  25.   // Member methods of the behavior
  26.   this.start = CameraRotateBehaviorStart;
  27.   this.stop = CameraRotateBehaviorStop;
  28.  
  29.   this.camera = camera;
  30.   this.intensity = intensity;
  31.   this.dragID = TSMakeUniqID("DragForce_" + camera);
  32.  
  33.   TSMakeDragForce(this.dragID, intensity, '0 0 0',
  34.                   TSMakeStringFromPoint(TSSolidGetPosition(camera)));
  35.  
  36.   this.dampingID = TSMakeUniqID("DampingForce_" + camera);
  37.   TSMakeDampingSolidForce(this.dampingID, "1.0", "1.0");
  38.  
  39.   TSAppendChild(camera, this.dragID);
  40.   TSAppendChild(camera, this.dampingID);
  41.  
  42.   TSUpdateNode(camera);
  43.  
  44.   // Free the camera
  45.   TSUpdateNodeAttribute(camera, 'fixed', '0');
  46. }
  47.  
  48. function CameraRotateBehaviorStart(direction, step)
  49. {
  50.   // Get the position of the camera dragger solid
  51.   var camPosition = TSCameraGetPosition(this.camera);
  52.   // Get the dragger target point
  53.   var tgtPosition = TSCameraGetTargetPosition(this.camera);
  54.     
  55.   // Create the vector between the camera and the target point ( in spherical coordinates )
  56.   var sphericCoords = TSVectorToSphericCoords(TSMakeVector(tgtPosition, camPosition));
  57.  
  58.   // Compute the  current rotation angles
  59.   switch (direction)
  60.   {
  61.   case "right":
  62.     sphericCoords.theta += TSDegToRad(step);
  63.     break;
  64.     
  65.   case "left":
  66.     sphericCoords.theta -= TSDegToRad(step);
  67.     break;
  68.     
  69.   case "up":
  70.     newPhi = sphericCoords.phi + TSDegToRad(step);
  71.     if (newPhi < phiLimit && newPhi < TSDegToRad(phiMax)) {
  72.         sphericCoords.phi = newPhi;
  73.     }
  74.     break;
  75.     
  76.   case "down":
  77.     newPhi = sphericCoords.phi - TSDegToRad(step);
  78.     if (newPhi > -phiLimit && newPhi > TSDegToRad(phiMin)) {
  79.       sphericCoords.phi = newPhi;
  80.     }
  81.       break;
  82.   }
  83.  
  84.   var position = TSSphericCoordsToVector(sphericCoords);
  85.   position = TSPointTranslate(position, tgtPosition.x, tgtPosition.y, tgtPosition.z);
  86.  
  87.   // Move the camera dragger at the new location
  88.   TSUpdateNodeAttribute(this.dragID, "targetPoint", TSMakeStringFromPoint(position));
  89. }
  90.  
  91. function CameraRotateBehaviorStop(camera)
  92. {
  93. }
  94.  
  95. //
  96. // Event functions (public)
  97. //
  98.  
  99. function CameraRotateBehaviorStartEvent(obj, event)
  100. {
  101.   var camera = TSGetExtraParam(event, 'camera');
  102.   var speed = TSGetExtraParam(event, 'speed');
  103.   var direction = TSGetExtraParam(event, 'direction');
  104.   var step = TSGetExtraParam(event, 'step');
  105.  
  106.   if (rotateCameras[camera] == null) {
  107.     rotateCameras[camera] = new CameraRotateBehavior(camera, parseFloat(speed) * 10);
  108.   }
  109.  
  110.   rotateCameras[camera].start(direction, step);
  111. }
  112.  
  113. function CameraRotateBehaviorStopEvent(obj, event)
  114. {
  115. }
  116.